#!/bin/bash

members=("node1" "node2" "node3")
pub_addr=("172.24.16.148" "172.24.16.149" "172.24.16.151")
cls_addr=("10.64.5.1" "10.64.5.2" "10.64.5.3")
ceph_cfg="/etc/ceph/ceph.conf"

## fetch all nodes' OSD informations
ceph osd tree | awk '$0 ~ /(host|osd)/ {print $4}' > /tmp/osd.inf

## add OSD information to ceph configuration file.
osd_host=" "; member_idx=-1
cat /tmp/osd.inf | while read LINE  
do
if [[ ${LINE:0:4} == "node" ]] ; then 
	osd_host=$LINE
	member_idx=$(($member_idx+1))
elif [[ ${LINE:0:3} == "osd" ]] ; then 
	echo " " >> $ceph_cfg
	echo "["$LINE"]" >> $ceph_cfg
	echo "host = $osd_host" >> $ceph_cfg
	echo "public_addr = ${pub_addr[$member_idx]}" >> $ceph_cfg
	echo "cluster_addr = ${cls_addr[$member_idx]}" >> $ceph_cfg
fi
done

## restart all nodes' mon,mgr,mds
for((i=0; i<${#members[@]}; i++))
do
    cmd="systemctl restart ceph-mon@${members[$i]}"; ssh root@${members[$i]} $cmd
    cmd="systemctl restart ceph-mgr@${members[$i]}"; ssh root@${members[$i]} $cmd
    cmd="systemctl restart ceph-mds@${members[$i]}"; ssh root@${members[$i]} $cmd
done

## restart all node's OSD 
osd_host=" "; member_idx=-1
cat /tmp/osd.inf | while read LINE  
do
if [[ ${LINE:0:4} == "node" ]] ; then 
	osd_host=$LINE
	member_idx=$(($member_idx+1))
elif [[ ${LINE:0:3} == "osd" ]] ; then 
	array=(${LINE//./ })
	osd_idx=${array[1]}
	cmd="systemctl restart ceph-osd@$osd_idx"
	ssh root@$osd_host $cmd
	#echo $osd_host"->"$cmd
fi
done